<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>用药错误不良事件上报粗略表</title>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <script src="../layui/layui.js"></script>
</head>
<body>

<div class="site-text" style="margin: 5%; display: none" id="check"  target="test123">
    <form class="layui-form" lay-filter="FormLoad" id="event">
        <table border='1px' width='100%' cellpadding='0' id="used">
        </table>
        <div class="layui-form-item" id="suggestion" style="display: none">
            <label class="layui-form-label">处理意见</label>
            <div class="layui-input-block">
                <textarea id="handle_suggestion" name="handle_suggestion" style="height: 80px"  lay-verify="" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item" id="button" style="display: none">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="handle">提交</button>
                <button class="layui-btn" lay-submit lay-filter="not">取消</button>
            </div>
        </div>
    </form>
</div>

<form class="layui-form" style="margin: 20px;text-align:center ">
    <div class="layui-form-item" style="">
        <div class="layui-inline" style="margin-top: 10px">
            <div class="layui-input-block" >
                <button class="layui-btn" lay-submit lay-filter="*">查询</button>
            </div>
        </div>
        <div class="layui-inline" style="margin-top: 10px">
            <label class="layui-form-label">状态查询： </label>
            <div class="layui-input-inline" >
                <select name="modules" id="test" lay-filter="test" lay-search="" lay-verify="">
                    <option value="0" selected></option>
                    <option value="1">草稿</option>
                    <option value="2">已提交</option>
                </select>
            </div>
        </div>
        <div class="layui-inline" style="margin-top: 10px">
            <label class="layui-form-label">部门： </label>
            <div class="layui-input-inline" >
                <select name="modules" id="test0" lay-filter="test0" lay-search="" lay-verify="">
                    <option value="0" selected></option>
                    <option value="1">产科</option>
                    <option value="2">护理科</option>
                    <option value="3">院感科</option>
                </select>
            </div>
        </div>
        <!--        <div class="layui-inline" style="margin-top: 10px">-->
        <!--            <label class="layui-form-label">伤害程度： </label>-->
        <!--            <div class="layui-input-inline" style="">-->
        <!--                <select name="modules" id="test1" lay-filter="test1" lay-search="" lay-verify="">-->
        <!--                    <option value="0" selected>无</option>-->
        <!--                    <option value="1">A,没有，仅有导致伤害的潜在因素，存在不安全条件</option>-->
        <!--                    <option value="2">B.没有，事件差一点导致伤害但被及时避免，有可能导致伤害但没有延及病人</option>-->
        <!--                    <option value="3">C.没有，事件延及病人但没有造成伤害</option>-->
        <!--                    <option value="4">D、是的，一般伤害(密切监护，积极干预后对病人未造成不良后果)</option>-->
        <!--                    <option value="5">E、其他</option>-->
        <!--                </select>-->
        <!--            </div>-->
        <!--        </div>-->
        <!--        <div class="layui-inline" style="margin-top: 10px">-->
        <!--            <label class="layui-form-label">处理人： </label>-->
        <!--            <div class="layui-input-inline" style="" >-->
        <!--                <input name="name" value="无" id="test2" type="text" class="layui-input">-->
        <!--            </div>-->
        <!--        </div>-->
        <div class="layui-inline" style="margin-top: 10px">
            <label class="layui-form-label">报告日期： </label>
            <div class="layui-input-inline" style="" >
                <input name="date" value="" id="test3" type="text" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="margin-top: 10px">
            <label class="layui-form-label">报告人： </label>
            <div class="layui-input-inline" style="" >
                <input name="name" value="" id="test4" type="text" class="layui-input">
            </div>
        </div>
    </div>

</form>
<table id="demo" lay-filter="test"></table>

</body>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="select">查看</a>
</script>

<script type="text/html" id="index">
    {{d.LAY_TABLE_INDEX+1}}
</script>
<script>

    function getQueryVariable(variable)
    {
        let query = window.location.search.substring(1);
        let vars = query.split("&");
        for (let i=0;i<vars.length;i++) {
            let pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }
    layui.use(['table','form','jquery','layer'], function(){
        let table = layui.table;
        let form = layui.form;
        let $ = layui.$;
        let layer = layui.layer;
        let router = layui.router();
        // 获取地址的中的值
        let user_code=decodeURIComponent(getQueryVariable("user_code"));
        let user_name=decodeURIComponent(getQueryVariable("user_name"));
        let dept_code=decodeURIComponent(getQueryVariable("dept_code"));
        let dept_name=decodeURIComponent(getQueryVariable("dept_name"));
        // layui data 保存数据
        if( user_code=="undefined"){
            console.log(layui.data('user').userinfo.user_name)
        } else{
            console.log(user_code);
            console.log("地址有值")
            layui.data('user', {
                key: 'userinfo',
                value:
                    {
                        user_name: user_name,
                        user_code: user_code,
                        dept_code:dept_code,
                        dept_name:dept_name
                    }
            });
            console.log(layui.data('user').userinfo.user_name)
        }
        let reporter_code = layui.data('user').userinfo.user_code;
        //表格渲染
        table.render({
            elem: '#demo'
            ,height: 312
            ,url: '/admin/ALL?event_code=3' //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {title: '序号', fixed: 'left', fixed: 'left', templet: '#index'}
                ,{field: 'patient_name', title: '患者名字'}
                ,{field: 'bad_event_name', title: '事件名称'}
                ,{field: 'bad_event_happen_time', title: '报告时间'}
                ,{field: 'kind_name', title: '事件种类'}
                ,{field: 'status_name', title: '事件状态'}
                ,{fixed: 'right', title:'操作', align:'center', toolbar: '#barDemo'}
            ]],
        });
        //监听工具条
        table.on('tool(test)', function(obj){
            let layEvent = obj.event;
            if(layEvent === 'select'){
                let url ="http://172.16.88.106:8080/select/YYCW_select_event.html?form_code="+obj.data.form_code;
                window.location=url;
            } else if(layEvent === 'handle') {
                //弹出层
                let button = document.getElementById("button");
                button.style.display = "inline";
                let suggestion = document.getElementById("suggestion");
                suggestion.style.display = "inline";
                layer.closeAll();
                layer.open({
                    type: 1 //Page层类型
                    ,skin: 'layui-layer-molv'
                    ,area: ['1000px', '800px']
                    ,title: ['处理意见','font-size:18px']
                    ,shadeClose: true
                    ,shade: 0 //遮罩透明度
                    ,maxmin: true //允许全屏最小化
                    ,content:$("#check")  //弹窗路径
                    ,success:function(){
                        $.ajax({
                            url:"/look?"+'form_code='+obj.data.form_code,
                            type: 'get',
                            success:function(data){
                                // console.log(data);
                                let jsonObj = eval('(' + data + ')'); //获得jsonObj对象
                                console.log(jsonObj);
                                for (let i=0; i < jsonObj.data.length; i++) {
                                    $("#used").append("<tr><div class='layui-form-item'><td width='200px'>" + jsonObj.data[i].property_name + "</td><td colspan='5'><input type='text' disabled  placeholder= '' class='layui-input' name='"+jsonObj.data[i].property_en_name+"' value=' " + jsonObj.data[i].detailed_data + "'/></td></div></tr>");

                                }
                            }
                        });
                    }

                });
                form.on('submit(handle)', function (data) {
                    layer.confirm('确定提交吗？', {
                        btn: ['确认', '取消'] //按钮
                    }, function () {

                        let json = {};
                        let newObj = "";

                        json.form_code = obj.data.form_code;
                        json.reporter_dept = "医务科";

                        json.suggestion = form.val("FormLoad").handle_suggestion;

                        $.ajax({
                            url: '/event/event_handle',
                            type: "POST",
                            data: json,
                            success: function (index) {
                                layer.msg("处理成功");
                                button.style.display = "none";
                                location.reload();
                            }
                        })

                    }, function () {
                    });
                    console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
                    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                });

                $("#used").empty();
            }
        });
        let number = ["", "", "", ""];
        //选择框监听
        form.on('select(test)', function (data) {
            // number[0] = data.elem[data.elem.selectedIndex].text;
            if (data.value == 0)
                number[0] = " ";
            else
                number[0] = data.value;
            // console.log(number[0]);
        });
        form.on('select(test0)', function (data) {
            number[1] = data.elem[data.elem.selectedIndex].text;
            console.log(number[1]);
        });
        // form.on('select(test1)', function (data) {
        //     number[2] = data.elem[data.elem.selectedIndex].text;
        // });
        //查询按钮监听
        form.on('submit(*)', function (data) {
            //let i = 0;
            //console.log(data.field.name);
            number[2] = data.field.date;
            number[3] = data.field.name;
            // while(number[i]!=null&&number[i]=="无")
            // {
            //     console.log(number[i]);
            //       number[i]="";
            //       i++;
            // }
            table.render({
                elem: '#demo'
                ,
                height: 312
                ,
                url: '/admin/part?event_code=3'+'&status='+number[0]+'&dept_name=' + number[1]+'&report_date='+number[2]+'&reporter='+number[3] //数据接口
                ,
                page: true //开启分页
                ,
                cols: [[ //表头
                    {title: '序号', fixed: 'left', fixed: 'left', templet: '#index'}
                    ,{field: 'patient_name', title: '患者名字'}
                    ,{field: 'bad_event_name', title: '事件名称'}
                    ,{field: 'bad_event_happen_time', title: '报告时间'}
                    ,{field: 'kind_name', title: '事件种类'}
                    ,{field: 'status_name', title: '事件状态'}
                    ,{fixed: 'right', title:'操作', align:'center', toolbar: '#barDemo'}
                ]],
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //表单重载
        form.render();
    });
</script>

</html>